package view.projectTeam;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.JsonObject;

import dal.project_team.ProjectTeamMember;
import dal.project_team.ProjectTeamMemberUtil;

/**
 * Servlet implementation class AddProjectTeamMember
 */
@WebServlet("/AddProjectTeamMember")
public class AddProjectTeamMember extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AddProjectTeamMember() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		PrintWriter printWriter = response.getWriter();
		response.setContentType("text/HTML");

		response.setHeader("Cache-control", "no-cache, no-store");
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Expires", "-1");

		response.setHeader("Access-Control-Allow-Origins", "*");
		response.setHeader("Access-Control-Allow-Methods", "POST");
		response.setHeader("Access-Control-Allow-Headers", "Content-Type");
		response.setHeader("Access-Control-Max-Age", "86400");

		String TeamId = "";
		JsonObject myJsonObj = new JsonObject();

		try {

			ProjectTeamMemberUtil projectTeamMemberUtil = new ProjectTeamMemberUtil();
			ProjectTeamMember projectTeamMember = new ProjectTeamMember();
			TeamId = request.getParameter("TeamId");
			String EmployeeId = request.getParameter("EmployeeId");
			String Role = request.getParameter("Role");

			System.out.println("In Servlet \n" + TeamId + "" + EmployeeId + ""
					+ Role);
			//Result = TeamId + "" + EmployeeId + "" + Role;
			projectTeamMember.setEmployeeId(EmployeeId);
			projectTeamMember.setRole(Role);
			projectTeamMember.setTeamId(TeamId);

			Boolean isOk = projectTeamMemberUtil.add(projectTeamMember);

		} catch (Exception e) {
			e.printStackTrace();
		}
		myJsonObj.addProperty("TeamId",TeamId);

		System.out.println(TeamId);
		System.out.println(myJsonObj);

		printWriter.println(myJsonObj.toString());
		printWriter.close();

	}

}
